//
//  CNAppEntranceController.m
//  CarNote
//
//  Created by linshili on 13-9-29.
//  Copyright (c) 2013年 linshili. All rights reserved.
//

#import "CNAppEntranceController.h"
#import "CNGasChartView.h"
#import "CNGasInputController.h"
#import "CNGasDataManager.h"
#import "CNServiceDataManager.h"
#import "CNGlobal.h"

// 默认行数
#define DEFAULT_ROWS_GASTABLE 4
#define DEFAULT_ROWS_SERVICETABLE 2

// 最多可以下拉扩展的行数
#define EXPAND_MAX_ROWS_GASTABLE 10
#define EXPAND_MAX_ROWS_SERVICETABLE 10

// 表格宽高定义
#define GASTALBE_ROW_HEIGHT (44.0)
#define SERVICETABLE_ROW_HEIGHT (88.0)
#define TABLE_FOOTER_HEIGHT (30.0)



@interface CNAppEntranceController () <UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *gasTableView;
@property (nonatomic, strong) UITableView *serviceTableView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) CNGasChartView *gasChartView;

@property (nonatomic, strong) UIButton *addGasButton;
@property (nonatomic, strong) UIButton *addServiceButton;

// 实际下拉扩展的表格行数
@property (nonatomic) NSInteger expandRowsInGasTable;
@property (nonatomic) NSInteger expandRowsInServiceTable;

// 三个数据展示块的原点Y坐标，相对于scrollView
@property (nonatomic) CGFloat gasTableOriginY;
@property (nonatomic) CGFloat serviceTableOriginY;
@property (nonatomic) CGFloat gasChartOriginY;
@property (nonatomic) CGFloat addGasButtonHeight;
@property (nonatomic) CGFloat addServiceHeight;

// 加油表和保养表的底部“更多”操作，当数据不够时，高度为0，不展示
@property (nonatomic) CGFloat gasTableFooterHeight;
@property (nonatomic) CGFloat serviceTableFooterHeight;


@end

@implementation CNAppEntranceController


- (id)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(userHasInputNewData:)
                                                     name:kUserHasInputNewDataNotification
                                                   object:nil];
        
    }
    return self;
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if (OSVersionIsAtLeastiOS7()) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_app"]];
    [self.view addSubview:bgView];

    _scrollView = [[UIScrollView alloc] initWithFrame:[self.view bounds]];
    [_scrollView setDelegate:self];
    [_scrollView setBackgroundColor:[UIColor clearColor]];
    [_scrollView setScrollEnabled:YES];
    [_scrollView setContentSize:CGSizeMake(320, 800)];
    _scrollView.showsVerticalScrollIndicator = FALSE;
    [self.view addSubview:_scrollView];
    
    _expandRowsInGasTable = 0;
    _expandRowsInServiceTable = 0;
    
    if ([[CNGasDataManager shareInstance] gasInfoCount] > DEFAULT_ROWS_GASTABLE) {
        _gasTableFooterHeight = TABLE_FOOTER_HEIGHT;
    }
    else {
        _gasTableFooterHeight = 0;
    }
    
    if ([[CNServiceDataManager shareInstance] serviceInfoCount] > DEFAULT_ROWS_SERVICETABLE) {
        _serviceTableFooterHeight = TABLE_FOOTER_HEIGHT;
    }
    else {
        _serviceTableFooterHeight = 0;
    }
    
    [self loadNavigationViews];
    
    if ([self isGasDataAvaliable]) {
        [self loadGasTableViewOnScrollView];
    }
    else {
        [self loadAddGasButton];
    }
    
//    if ([self isServiceDataAvaliable]) {
//        [self loadServiceTableViewOnScrollView];
//    }
//    else {
//        [self loadAddServiceButton];
//    }
//    
//    if ([self isGasDataAvaliable]) {
//        [self loadGasChartOnScrollView];
//    }

}


- (void)loadNavigationViews
{
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                               target:self
                                                                               action:@selector(dataInputController)];
    self.navigationItem.rightBarButtonItem = rightItem;
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];

    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                                                              target:nil
                                                                              action:nil];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"卡诺" ;

}


- (void)loadGasTableViewOnScrollView
{
    _gasTableOriginY = 72.0;
    
    CGRect gasTableRect = CGRectMake(4, _gasTableOriginY, 312, DEFAULT_ROWS_GASTABLE * GASTALBE_ROW_HEIGHT + _gasTableFooterHeight);
    _gasTableView = [[UITableView alloc] initWithFrame:gasTableRect style:UITableViewStylePlain];
    [_gasTableView setDataSource:self];
    [_gasTableView setDelegate:self];
    [_gasTableView setBackgroundColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_table"]] colorWithAlphaComponent:0.7]];
    [_gasTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [_gasTableView setSeparatorColor:[UIColor whiteColor]];
    // 表格隔断线内边距左右各收缩5个point
    [_gasTableView setSeparatorInset:UIEdgeInsetsMake(0, 5, 0, 5)];
    
    [_scrollView addSubview:_gasTableView];
    
}


- (void)loadServiceTableViewOnScrollView
{
    _serviceTableOriginY = _gasTableOriginY + _gasTableView.frame.size.height + 15;
    
    CGRect tableRect = CGRectMake(4, _serviceTableOriginY, 312, DEFAULT_ROWS_SERVICETABLE * SERVICETABLE_ROW_HEIGHT + _serviceTableFooterHeight);
    _serviceTableView = [[UITableView alloc] initWithFrame:tableRect style:UITableViewStylePlain];
    [_serviceTableView setDataSource:self];
    [_serviceTableView setDelegate:self];
    [_serviceTableView setBackgroundColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_table"]] colorWithAlphaComponent:0.7]];
    [_serviceTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [_serviceTableView setSeparatorColor:[UIColor whiteColor]];
    // 表格隔断线内边距左右各收缩5个point
    [_serviceTableView setSeparatorInset:UIEdgeInsetsMake(0, 5, 0, 5)];
    
    [_scrollView addSubview:_serviceTableView];
}

- (void)loadGasChartOnScrollView
{
    if ([self isServiceDataAvaliable]) {
        _gasChartOriginY = _serviceTableOriginY + _serviceTableView.frame.size.height + 15;
    }
    else {
        _gasChartOriginY = _serviceTableOriginY + _addServiceHeight + 15;
    }
    
    CGRect chartRect = CGRectMake(4, _gasChartOriginY, 312, 206);
    _gasChartView = [[CNGasChartView alloc] initWithFrame:chartRect];

    [_scrollView addSubview:_gasChartView];
}


- (void)dataInputController
{
    CNGasInputController *gasInputController = [[CNGasInputController alloc] init];
    [self.navigationController pushViewController:gasInputController animated:YES];
}

// 切换加油表格下拉收起、扩展
- (void)toggleGasTableFlodExpand:(UIButton *)sender
{
    if (_expandRowsInGasTable == 0) {
        // do expand
        NSInteger count = [[CNGasDataManager shareInstance] gasInfoCount];
        if (count > EXPAND_MAX_ROWS_GASTABLE + DEFAULT_ROWS_GASTABLE) {
            _expandRowsInGasTable = EXPAND_MAX_ROWS_GASTABLE;
        }
        else {
            _expandRowsInGasTable = count - DEFAULT_ROWS_GASTABLE;
        }

        // reposition views
        CGFloat expandHeight = _expandRowsInGasTable * GASTALBE_ROW_HEIGHT;
        
        CGRect gasTableFrame = _gasTableView.frame;
        gasTableFrame.size.height += expandHeight;
        _gasTableView.frame = gasTableFrame;
        
        _serviceTableView.frame = CGRectOffset(_serviceTableView.frame, 0, expandHeight);
        _gasChartView.frame = CGRectOffset(_gasChartView.frame, 0, expandHeight);
        
        
//        // insert rows        
//        NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init];
//        for (int i = 0; i < _expandRowsInGasTable; i++) {
//            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(DEFAULT_ROWS_GASTABLE + i)  inSection:0];
//            [insertIndexPaths addObject:indexPath];
//        }
//        [_gasTableView beginUpdates];
//        [_gasTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
//        [_gasTableView endUpdates];
        
        [sender setTitle:@"收起 ▲" forState:UIControlStateNormal];
    }
    else {
        // do fold
        // reposition views
        CGFloat expandHeight = _expandRowsInGasTable * GASTALBE_ROW_HEIGHT;
        
        CGRect gasTableFrame = _gasTableView.frame;
        gasTableFrame.size.height -= expandHeight;
        _gasTableView.frame = gasTableFrame;
        
        _serviceTableView.frame = CGRectOffset(_serviceTableView.frame, 0, -expandHeight);
        _gasChartView.frame = CGRectOffset(_gasChartView.frame, 0, -expandHeight);
        
        // delete rows
//        NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init];
//        for (int i = 0; i < _expandRowsInGasTable; i++) {
//            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(DEFAULT_ROWS_GASTABLE + i)  inSection:0];
//            [deleteIndexPaths addObject:indexPath];
//        }
//        
        _expandRowsInGasTable = 0;
//
//        [_gasTableView beginUpdates];
//        [_gasTableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
//        [_gasTableView endUpdates];
        [sender setTitle:@"更多 ▼" forState:UIControlStateNormal];
    }
}

// 切换服务表格下拉收起、扩展
- (void)toggleServiceTableFlodExpend:(UIButton *)sender
{
    if (_expandRowsInServiceTable == 0) {
        // do expand
        NSInteger count = [[CNServiceDataManager shareInstance] serviceInfoCount];
        if (count > EXPAND_MAX_ROWS_SERVICETABLE + DEFAULT_ROWS_SERVICETABLE) {
            _expandRowsInServiceTable = EXPAND_MAX_ROWS_SERVICETABLE;
        }
        else {
            _expandRowsInServiceTable = count - DEFAULT_ROWS_SERVICETABLE;
        }
        
        // reposition views
        CGFloat expandHeight = _expandRowsInServiceTable * SERVICETABLE_ROW_HEIGHT;
        
        CGRect serviceTableFrame = _serviceTableView.frame;
        serviceTableFrame.size.height += expandHeight;
        _serviceTableView.frame = serviceTableFrame;
        
        _gasChartView.frame = CGRectOffset(_gasChartView.frame, 0, expandHeight);
        
        // insert rows
//        NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init];
//        for (int i = 0; i < _expandRowsInServiceTable; i++) {
//            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(DEFAULT_ROWS_SERVICETABLE + i)  inSection:0];
//            [insertIndexPaths addObject:indexPath];
//        }
//        [_serviceTableView beginUpdates];
//        [_serviceTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
//        [_serviceTableView endUpdates];
        
        [sender setTitle:@"收起 ▲" forState:UIControlStateNormal];
    }
    else {
        // do fold
        // reposition views
        CGFloat expandHeight = _expandRowsInServiceTable * SERVICETABLE_ROW_HEIGHT;
        
        CGRect serviceTableFrame = _serviceTableView.frame;
        serviceTableFrame.size.height -= expandHeight;
        _serviceTableView.frame = serviceTableFrame;
        
        _gasChartView.frame = CGRectOffset(_gasChartView.frame, 0, -expandHeight);
        
        // delete rows
//        NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init];
//        for (int i = 0; i < _expandRowsInServiceTable; i++) {
//            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(DEFAULT_ROWS_SERVICETABLE + i)  inSection:0];
//            [deleteIndexPaths addObject:indexPath];
//        }
//        
        _expandRowsInServiceTable = 0;
//
//        [_serviceTableView beginUpdates];
//        [_serviceTableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
//        [_serviceTableView endUpdates];
        [sender setTitle:@"更多 ▼" forState:UIControlStateNormal];
    }
}


- (void)userHasInputNewData:(NSNotification *)notification
{
    //replace add buttons if needed
    if ([[CNGasDataManager shareInstance] gasInfoCount] == 1) {
        [_addGasButton removeFromSuperview];
        [self loadGasTableViewOnScrollView];
    }
    
    if ([[CNServiceDataManager shareInstance] serviceInfoCount] == 1) {
        [_addServiceButton removeFromSuperview];
        [self loadServiceTableViewOnScrollView];
    }
    
    
    // 加油表 footer, ajust views' positon, reload data
    if ([[CNGasDataManager shareInstance] gasInfoCount] > DEFAULT_ROWS_GASTABLE && _gasTableFooterHeight == 0) {
        // need add footer
        _gasTableFooterHeight = TABLE_FOOTER_HEIGHT;
        
        CGRect gasTableFrame = _gasTableView.frame;
        gasTableFrame.size.height += _gasTableFooterHeight;
        _gasTableView.frame = gasTableFrame;
        
        _serviceTableView.frame = CGRectOffset(_serviceTableView.frame, 0, _gasTableFooterHeight);
        _gasChartView.frame = CGRectOffset(_gasChartView.frame, 0, _gasTableFooterHeight);
        _addServiceButton.frame = CGRectOffset(_addServiceButton.frame, 0, _gasTableFooterHeight);
    }
    else if ([[CNGasDataManager shareInstance] gasInfoCount] < DEFAULT_ROWS_GASTABLE && _gasTableFooterHeight > 0) {
        // need hide footer
        CGRect gasTableFrame = _gasTableView.frame;
        gasTableFrame.size.height -= _gasTableFooterHeight;
        _gasTableView.frame = gasTableFrame;
        
        _serviceTableView.frame = CGRectOffset(_serviceTableView.frame, 0, -_gasTableFooterHeight);
        _gasChartView.frame = CGRectOffset(_gasChartView.frame, 0, -_gasTableFooterHeight);
        _addServiceButton.frame = CGRectOffset(_addServiceButton.frame, 0, -_gasTableFooterHeight);
        
        _gasTableFooterHeight = 0;
    }
    
    if (_gasTableView != nil) {
        [_gasTableView reloadData];
    }
    
    // 保养表footer, ajust views' positon, reload data
    if ([[CNServiceDataManager shareInstance] serviceInfoCount] > DEFAULT_ROWS_SERVICETABLE && _serviceTableFooterHeight == 0) {
        // need add footer
        _serviceTableFooterHeight = TABLE_FOOTER_HEIGHT;
        
        CGRect serviceTableFrame = _serviceTableView.frame;
        serviceTableFrame.size.height += _serviceTableFooterHeight;
        _serviceTableView.frame = serviceTableFrame;
        
        _gasChartView.frame = CGRectOffset(_gasChartView.frame, 0, _serviceTableFooterHeight);
    }
    else if ([[CNServiceDataManager shareInstance] serviceInfoCount] < DEFAULT_ROWS_SERVICETABLE && _serviceTableFooterHeight > 0) {
        // need hide footer
        CGRect serviceTableFrame = _serviceTableView.frame;
        serviceTableFrame.size.height -= _serviceTableFooterHeight;
        _serviceTableView.frame = serviceTableFrame;

        _gasChartView.frame = CGRectOffset(_gasChartView.frame, 0, -_serviceTableFooterHeight);
        
        _serviceTableFooterHeight = 0 ;
    }

    if (_serviceTableView != nil) {
        [_serviceTableView reloadData];
    }
    
}


// 加油表的cell
- (UITableViewCell *)cellForGasTable:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:@"gasTableViewCellIdentifier"];
    if (tableCell == nil) {
        tableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"gasTableViewCellIdentifier"];
        tableCell.frame = CGRectMake(0, 0, 310, 44);
        tableCell.backgroundColor = [UIColor clearColor];
        
        UILabel *createTimestampLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 14, 70, 20)];
        createTimestampLabel.text = @"2012/07/12";
        createTimestampLabel.font = [UIFont systemFontOfSize:12.0];
        createTimestampLabel.textColor = [UIColor whiteColor];
        [tableCell.contentView addSubview:createTimestampLabel];
        
        UILabel *odometerLabel = [[UILabel alloc] initWithFrame:CGRectMake(75, 10, 130, 25)];
        odometerLabel.text = @"102048";
        odometerLabel.font = [UIFont systemFontOfSize:20.0];
        odometerLabel.textColor = [UIColor whiteColor];
        [tableCell.contentView addSubview:odometerLabel];
        
        UILabel *KMLabel = [[UILabel alloc] initWithFrame:CGRectMake(145, 15, 20, 20)];
        KMLabel.text = @"KM";
        KMLabel.font = [UIFont systemFontOfSize:10.0];
        KMLabel.textColor = [UIColor whiteColor];
        [tableCell.contentView addSubview:KMLabel];
        
        UILabel *fillGasLabel = [[UILabel alloc] initWithFrame:CGRectMake(180, 10, 40, 25)];
        fillGasLabel.text = @"25.8";
        fillGasLabel.font = [UIFont systemFontOfSize:20.0];
        fillGasLabel.textColor = [UIColor whiteColor];
        [tableCell.contentView addSubview:fillGasLabel];
        
        UILabel *LLabel = [[UILabel alloc] initWithFrame:CGRectMake(222, 15, 20, 20)];
        LLabel.text = @"L";
        LLabel.font = [UIFont systemFontOfSize:12.0];
        LLabel.textColor = [UIColor whiteColor];
        [tableCell.contentView addSubview:LLabel];
        
        UILabel *moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(240, 10, 40, 25)];
        moneyLabel.text = @"25.8";
        moneyLabel.font = [UIFont systemFontOfSize:20.0];
        moneyLabel.textColor = [UIColor whiteColor];
        [tableCell.contentView addSubview:moneyLabel];
        
        UILabel *RMBLabel = [[UILabel alloc] initWithFrame:CGRectMake(281, 15, 30, 20)];
        RMBLabel.text = @"RMB";
        RMBLabel.font = [UIFont systemFontOfSize:10.0];
        RMBLabel.textColor = [UIColor whiteColor];
        [tableCell.contentView addSubview:RMBLabel];
    }

    //TODO: load from data source
    
    return tableCell;
}

// 保养表的cell
- (UITableViewCell *)cellForServiceTable:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableCell = [tableView dequeueReusableCellWithIdentifier:@"serviceTableViewCellIdentifier"];
    if (tableCell == nil) {
        tableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"serviceTableViewCellIdentifier"];
        tableCell.frame = CGRectMake(0, 0, 310, 44);
        tableCell.backgroundColor = [UIColor clearColor];
        
        UILabel *createTimestampLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 14, 70, 20)];
        createTimestampLabel.text = @"2012/07/12";
        createTimestampLabel.font = [UIFont systemFontOfSize:12.0];
        createTimestampLabel.textColor = [UIColor whiteColor];
        [tableCell.contentView addSubview:createTimestampLabel];
        
        UILabel *odometerLabel = [[UILabel alloc] initWithFrame:CGRectMake(75, 10, 130, 25)];
        odometerLabel.text = @"102048";
        odometerLabel.font = [UIFont systemFontOfSize:20.0];
        odometerLabel.textColor = [UIColor whiteColor];
        [tableCell.contentView addSubview:odometerLabel];
        
        UILabel *KMLabel = [[UILabel alloc] initWithFrame:CGRectMake(145, 15, 20, 20)];
        KMLabel.text = @"KM";
        KMLabel.font = [UIFont systemFontOfSize:10.0];
        KMLabel.textColor = [UIColor whiteColor];
        [tableCell.contentView addSubview:KMLabel];
        
        UILabel *moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(180, 10, 40, 25)];
        moneyLabel.text = @"25.8";
        moneyLabel.font = [UIFont systemFontOfSize:20.0];
        moneyLabel.textColor = [UIColor whiteColor];
        [tableCell.contentView addSubview:moneyLabel];
        
        UILabel *RMBLabel = [[UILabel alloc] initWithFrame:CGRectMake(222, 15, 30, 20)];
        RMBLabel.text = @"RMB";
        RMBLabel.font = [UIFont systemFontOfSize:10.0];
        RMBLabel.textColor = [UIColor whiteColor];
        [tableCell.contentView addSubview:RMBLabel];
        
        UILabel *serviceLabel = [[UILabel alloc] initWithFrame:CGRectMake(73, 38, 210, 50)];
        serviceLabel.text = @"更换机油 更换空气滤芯 四轮定位";
        serviceLabel.font = [UIFont systemFontOfSize:12.0];
        serviceLabel.textColor = [UIColor whiteColor];
        serviceLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        serviceLabel.numberOfLines = 3;
        [serviceLabel sizeToFit];
        
        [tableCell.contentView addSubview:serviceLabel];
    }
    
    //TODO: load from data source
    
    return tableCell;
    
}


- (BOOL)isGasDataAvaliable
{
    return [[CNGasDataManager shareInstance] gasInfoCount] == 0 ? NO : YES;
}

- (BOOL)isServiceDataAvaliable
{
    return [[CNServiceDataManager shareInstance] serviceInfoCount] == 0 ? NO : YES;
}

- (void)loadAddGasButton
{
    _gasTableOriginY = 72.0;
    _addGasButtonHeight = DEFAULT_ROWS_GASTABLE * GASTALBE_ROW_HEIGHT + _gasTableFooterHeight;
    
    CGRect gasTableRect = CGRectMake(4, _gasTableOriginY, 312, _addGasButtonHeight);
    _addGasButton = [[UIButton alloc] initWithFrame:gasTableRect];
    _addGasButton.backgroundColor = [UIColor clearColor];
    [_addGasButton setImage:[UIImage imageNamed:@"addgas_btn"] forState:UIControlStateNormal];
    [_addGasButton setAlpha:0.7];
    [_addGasButton addTarget:self action:@selector(dataInputController) forControlEvents:UIControlEventTouchUpInside];
    
    [_scrollView addSubview:_addGasButton];
}

- (void)loadAddServiceButton
{
    if ([self isGasDataAvaliable]) {
        _serviceTableOriginY = _gasTableOriginY + _gasTableView.frame.size.height + 15;
    }
    else {
        _serviceTableOriginY = _gasTableOriginY + _addGasButtonHeight + 15;
    }
    _addServiceHeight = DEFAULT_ROWS_SERVICETABLE * SERVICETABLE_ROW_HEIGHT + _serviceTableFooterHeight;
    
    CGRect tableRect = CGRectMake(4, _serviceTableOriginY, 312, _addServiceHeight);
    _addServiceButton = [[UIButton alloc] initWithFrame:tableRect];
    _addServiceButton.backgroundColor = [UIColor clearColor];
    [_addServiceButton setImage:[UIImage imageNamed:@"addservice_btn"] forState:UIControlStateNormal];
    [_addServiceButton setAlpha:0.7];
    [_addServiceButton addTarget:self action:@selector(dataInputController) forControlEvents:UIControlEventTouchUpInside];
    
    [_scrollView addSubview:_addServiceButton];
}



#pragma mark - (Table DataSource / Delegate)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _gasTableView) {
        NSLog(@"gas info count:%d", [[CNGasDataManager shareInstance] gasInfoCount]);
        return [[CNGasDataManager shareInstance] gasInfoCount];
    }
    else if (tableView == _serviceTableView){
        return [[CNServiceDataManager shareInstance] serviceInfoCount];
    }
    
    return 0;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == _gasTableView) {
        return 1;
    }
    else if (tableView == _serviceTableView) {
        return 1;
    }
    
    return 0;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _gasTableView) {
        return GASTALBE_ROW_HEIGHT;
    }
    else if (tableView == _serviceTableView) {
        return SERVICETABLE_ROW_HEIGHT;
    }
    return 44.0;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    NSLog(@"tableView:heightForFooterInSection:");
    if (tableView == _gasTableView) {
        return _gasTableFooterHeight;
    }
    else if( tableView == _serviceTableView) {
        return _serviceTableFooterHeight;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    //return a footer button
    if (tableView == _gasTableView) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 310, TABLE_FOOTER_HEIGHT)];
        [button setBackgroundColor:[UIColor clearColor]];
        [button setTitle:@"更多 ▼" forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:11];
        [button addTarget:self action:@selector(toggleGasTableFlodExpand:) forControlEvents:UIControlEventTouchUpInside];
        return button;
    }
    else if (tableView == _serviceTableView) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 310, TABLE_FOOTER_HEIGHT)];
        [button setBackgroundColor:[UIColor clearColor]];
        [button setTitle:@"更多 ▼" forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:11];
        [button addTarget:self action:@selector(toggleServiceTableFlodExpend:) forControlEvents:UIControlEventTouchUpInside];
        return button;
    }
    
    return nil;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _gasTableView) {
        return [self cellForGasTable:tableView withIndexPath:indexPath];
        
    }
    else if (tableView == _serviceTableView) {
        return [self cellForServiceTable:tableView withIndexPath:indexPath];
    }
    
    return nil;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}



@end
